package Exercise.Observable;

import java.util.ArrayList;
import java.util.List;

/**
 * Description：
 * Author: zhangc
 * Date：2017/9/9 10:07
 */
public class Observable {

    private List<Observed> observeds;
    private boolean changed;

    public Observable() {
        this.observeds = new ArrayList<>();
        this.changed = false;
    }

    public void addObserver(Observed observed) {
        observeds.add(observed);
    }

    public void setChanged() {
        this.changed = true;
    }

    public void clear() {
        this.changed = false;
    }

    public void updateAll(Object obj) {
        if (changed) {
            for (Observed observed : observeds) {
                observed.update(this, obj);
            }
            changed = false;
        }
    }

    public void updateAll() {
        updateAll(null);
    }

}
